package com.ruoyi.common.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ruoyi.common.utils.bean.WeixinAccount;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtil
{
  public static final String REDIS_WX_ACCOUNT_LIST_KEY = "RES_KEY_WX_ACCOUNT_MAP";
  public static final String REDIS_WX_SUCAI_LINK_PRE = "RES_KEY_SUCAI_LINK_PRE";
  public static final String REDIS_WX_USER_KEY_PRE = "RES_KEY_WX_USER_PRE";
  private static final Logger logger = LoggerFactory.getLogger(JedisPoolUtil.class);
  private static JedisPool pool;
  private static String password;
  private static ResourceBundle bundle;
  
  public static Jedis getJedis()
  {
    return (Jedis)pool.getResource();
  }
  
  public static void returnJedis(Jedis jedis)
  {
    pool.returnResource(jedis);
  }
  
  static
  {
    bundle = ResourceBundle.getBundle("redis");
    if (bundle == null) {
      throw new IllegalArgumentException("[redis.properties] is not found!");
    }
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(100); //  设置最大连接数
	config.setMaxIdle(10);//  设置最大空闲连接数
	pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")).intValue());
	password = bundle.getString("redis.password");
    //Integer.valueOf(bundle.getString("redis.port")).intValue()
  }
  
  public static void resetAllWxAccount(Map<String, WeixinAccount> newWxAccountMap)
  {
    Jedis jedis = (Jedis)pool.getResource();
    jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(newWxAccountMap));
    
    pool.returnResource(jedis);
  }
  
  public static void clearReis()
  {
    Jedis jedis = (Jedis)pool.getResource();
    jedis.del(new byte[][] { "RES_KEY_WX_ACCOUNT_MAP".getBytes() });
    
    Set s = jedis.keys("RES_KEY_SUCAI_LINK_PRE*");
    Iterator it = s.iterator();
    while (it.hasNext())
    {
      String key = (String)it.next();
      logger.info("---------------------redis-------------key-------------------" + key);
      jedis.del(new String[] { key });
    }
    pool.returnResource(jedis);
  }
  
  public static void putWxUser(Map<String, String> userMap)
  {
    Jedis jedis = pool.getResource();
    jedis.auth(password);
    String openid = (String)userMap.get("openid");
    String key = "RES_KEY_WX_USER_PRE" + openid;
    jedis.set(key.getBytes(), SerializeUtil.serialize(userMap));
    
    pool.returnResource(jedis);
  }
  
  public static Map<String, String> getWxUser(String openid)
  {
    Jedis jedis = (Jedis)pool.getResource();
    jedis.auth(bundle.getString("redis.password"));
    String key = "RES_KEY_WX_USER_PRE" + openid;
    Map<String, String> returnMap = (Map)SerializeUtil.unserialize(jedis.get(key.getBytes()));
    
    pool.returnResource(jedis);
    return returnMap;
  }
  
  public static void putWeixinLinkOutUrl(String outUrl, String id)
  {
    if (RedisConvertUtils.isNotEmpty(outUrl))
    {
      Jedis jedis = (Jedis)pool.getResource();
      String key = "RES_KEY_SUCAI_LINK_PRE" + id;
      jedis.set(key, outUrl);
      
      pool.returnResource(jedis);
    }
  }
  
  public static String getWeixinLinkOutUrl(String id)
  {
    if (RedisConvertUtils.isNotEmpty(id))
    {
      Jedis jedis = (Jedis)pool.getResource();
      String key = "RES_KEY_SUCAI_LINK_PRE" + id;
      String outurl = jedis.get(key);
      
      pool.returnResource(jedis);
      return outurl;
    }
    return null;
  }
  
  public static Map<String, WeixinAccount> getAllWxAccount()
  {
    Jedis jedis = (Jedis)pool.getResource();
    Map<String, WeixinAccount> wxAccountMap = null;
    wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
    
    pool.returnResource(jedis);
    return wxAccountMap;
  }
  
  public static void putWxAccount(WeixinAccount po)
  {
    Jedis jedis = (Jedis)pool.getResource();
    jedis.auth(password);
    Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
    if (wxAccountMap == null) {
      wxAccountMap = new HashMap();
    }
    wxAccountMap.put(po.getWeixinAccountid(), po);
    jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(wxAccountMap));
    
    pool.returnResource(jedis);
  }
  
  public static void batchPutWxAccount(List<WeixinAccount> accountList)
  {
    Jedis jedis = (Jedis)pool.getResource();
    Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
    if (wxAccountMap == null) {
      wxAccountMap = new HashMap();
    }
    for (WeixinAccount po : accountList) {
      wxAccountMap.put(po.getWeixinAccountid(), po);
    }
    jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(wxAccountMap));
    
    pool.returnResource(jedis);
  }
  
  
  

  
  
  public static WeixinAccount getWxAccount(String jwid)
  {
    WeixinAccount po = null;
    try
    {
      Jedis jedis = (Jedis)pool.getResource();
      jedis.auth(password);
      
      Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
      if (wxAccountMap != null) {
        po = (WeixinAccount)wxAccountMap.get(jwid);
      }
      pool.returnResource(jedis);
    }
    catch (Exception e)
    {
      logger.error("--JedisPoolUtil--getWxAccount--error--" + e.toString());
    }
    return po;
  }
  
  public static void setList(String key, String... values)
  {
    Jedis jedis = (Jedis)pool.getResource();
    jedis.sadd(key, values);
    
    pool.returnResource(jedis);
  }
  
  public static boolean exitInList(String key, String value)
  {
    Jedis jedis = (Jedis)pool.getResource();
    boolean b = jedis.sismember(key, value).booleanValue();
    
    pool.returnResource(jedis);
    return b;
  }
  
  public static Object getList(String key)
  {
    Jedis jedis = (Jedis)pool.getResource();
    Set<String> smembers = jedis.smembers(key);
    
    pool.returnResource(jedis);
    return smembers;
  }
  
  public static Long clearList(String key)
  {
    Jedis jedis = (Jedis)pool.getResource();
    Long i = jedis.del(new String[] { key });
    
    pool.returnResource(jedis);
    return i;
  }
  
  public static void main(String[] args)
  {
    WeixinAccount wxAccount = getWxAccount("gh_61f6bb5a6620");
    System.out.println(wxAccount.getJsapiticket());
  }
  
  public static void maintest(String[] args)
  {
    Jedis jedis = (Jedis)pool.getResource();
    
    WeixinAccount po = new WeixinAccount();
    po.setId("sdfsdf-sdfsdfsdf");
    po.setAccountaccesstoken("100");
    po.setAccountappsecret("200");
    
    String keys = "name";
    
    jedis.set(po.getId().getBytes(), SerializeUtil.serialize(po));
    WeixinAccount tt = (WeixinAccount)SerializeUtil.unserialize(jedis.get(po.getId().getBytes()));
    System.out.println(tt.getAccountaccesstoken());
    
    pool.returnResource(jedis);
  }
}

